{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pymongo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "conn = pymongo.MongoClient()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第一种写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_id': ObjectId('5b2f2e24e0f42944105c81d2'),\n",
       " 'address': '浙江',\n",
       " 'age': 17.0,\n",
       " 'name': '张小二'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db = conn.chapter_3\n",
    "handler = db.example_data_1\n",
    "handler.find_one()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 第二种写法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_id': ObjectId('5b2f2e24e0f42944105c81d2'),\n",
       " 'address': '浙江',\n",
       " 'age': 17.0,\n",
       " 'name': '张小二'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db2 = conn['chapter_3']\n",
    "handler2 = db2['example_data_1']\n",
    "handler2.find_one()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用第二种写法可以批量处理多个数据库和多个集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_id': ObjectId('5b2f2e24e0f42944105c81d2'),\n",
       " 'address': '浙江',\n",
       " 'age': 17.0,\n",
       " 'name': '张小二'}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "db_name = 'chapter_3'\n",
    "collection_name = 'example_data_1'\n",
    "db3 = conn[db_name]\n",
    "handler3 = db3[collection_name]\n",
    "handler3.find_one()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f30a6e0f42944105c81d3'), 'name': '王小六', 'age': 25.0, 'work': ['工程师', '模特'], 'address': '苏州'}\n"
     ]
    }
   ],
   "source": [
    "row = handler.find_one({'age': 25})\n",
    "print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<pymongo.cursor.Cursor object at 0x10d2a7978>\n",
      "{'age': 25.0}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'age': 25, 'name': '慕容小九'}, {'name': 0, 'address': 0, '_id': 0})\n",
    "print(rows)\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MongoDB的基本语法——使用Python插入、更新、删除数据\n",
    "![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2019-02-16-10-28-52.png)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 插入数据\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "collection.insert_one(字典)  # 插入一条数据\n",
    "collection.insert_many(包含字典的列表)  # 批量插入多条数据\n",
    "```\n",
    "\n",
    "### 被插入的数据格式\n",
    "\n",
    "```\n",
    "{'field_1': value_1, 'field_2': value_2}\n",
    "\n",
    "[\n",
    " {'field_1': value_1, 'field_2': value_2},\n",
    " {'field_1': value_3, 'field_2': value_4}\n",
    "]\n",
    "```\n",
    "\n",
    "### 说明\n",
    "\n",
    "* MongoDB不需要提前创建数据库、不需要提前创建集合、不需要提取定义数据格式，想插入什么数据，直接插就行。\n",
    "* 同一个集合的不同行数据，字典可以不一样"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 插入一条数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertOneResult at 0x10d3d5a68>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'name': '青南', 'age': 26, 'address': '杭州'}\n",
    "handler.insert_one(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 插入一条数据，但是增加一个字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertOneResult at 0x10d3d5e58>"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = {'name': '青南2', 'age': 26, 'address': '杭州', 'salary': 99999}\n",
    "handler.insert_one(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertOneResult at 0x10d2afca8>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 在新的数据库、新的集合里面插入数据\n",
    "\n",
    "new_handler = conn.video_chapter3.example\n",
    "new_handler.insert_one({'name': '赵小一', 'birthday': '2019-02-16'})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 批量插入数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertManyResult at 0x10d3d5c60>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "data = [\n",
    "    {'name': '王十一', 'age': 25, 'address': '山东'},\n",
    "    {'name': '马十二', 'age': 24, 'address': '深圳'}\n",
    "]\n",
    "handler.insert_many(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 被批量插入的数据，每一条的格式也可以不一样"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertManyResult at 0x10d3d57e0>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [\n",
    "    {'name': '周十三', 'age': 23, 'address': '湖南'},\n",
    "    {'topic': '测试', 'price': 800, 'star': 3}\n",
    "]\n",
    "handler.insert_many(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更新数据\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "# 更新一条数据\n",
    "collection.update_one(查询条件, {'$set': 被更新的数据})\n",
    "\n",
    "# 更新所有满足要求的数据\n",
    "collection.update_many(查询条件, {'$set': 被更新的数据})\n",
    "```\n",
    "\n",
    "### 被更新的数据\n",
    "\n",
    "```\n",
    "{'field_1': value_1, 'field_2': value_2}\n",
    "```\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更新一条数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x10d3d58b8>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.update_one({'age': 25}, {'$set': {'address': '英国'}})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更新所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x10d3d0ee8>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.update_many({'age': 25}, {'$set': {'address': '火星'}})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 更新所有数据，并新增一列"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.UpdateResult at 0x10d3d5480>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.update_many({'age': 25}, {'$set': {'address': '火星', 'type': '火星人'}})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除数据\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "# 删除第一个满足条件的数据\n",
    "collection.delete_one(查询条件)\n",
    "\n",
    "# 删除所有满足条件的数据\n",
    "collection.delete_many(查询条件)\n",
    "```\n",
    "\n",
    "### 查询条件\n",
    "\n",
    "与查询数据的第一个参数完全相同\n",
    "\n",
    "### 建议\n",
    "\n",
    "建议先写查询语句，确认查询出来的数据就是自己想删除的数据，然后把关键字**find**改为**delete_one**或者**delete_many**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除一条数据\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "rows = handler.delete_one({'age': 25})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 删除所有数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.DeleteResult at 0x10d3da948>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.delete_many({'age': 25})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MongoDB的基本语法——条件查询和逻辑查询\n",
    "![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2019-02-16-10-28-52.png)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "handler = conn.chapter_3.example_data_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询并对结果进行计数\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "collection.find().count()\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.find({'grade': '五年级'}).count()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询并排序\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "# 在Python中\n",
    "\n",
    "collection.find().sort('字段名', 方向)\n",
    "\n",
    "# 在Robo 3T的命令输入区域\n",
    "\n",
    "collection.find().sort({'字段名': 方向})\n",
    "```\n",
    "\n",
    "其中方向为**1**表示升序，方向为**-1**表示降序"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询并对age升序排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f46'), 'name': '高六娃', 'age': 8, 'grade': '一年级', 'student': True, 'interest': '学习'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f41'), 'name': '李大娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '唱歌'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f47'), 'name': '赵气娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '乐高'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f42'), 'name': '张二娃', 'age': 12, 'grade': '六年级', 'student': True, 'interest': '跳舞'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f43'), 'name': '马三娃', 'age': 14, 'grade': '八年级', 'student': True, 'interest': '下棋'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f44'), 'name': '刘四娃', 'age': 16, 'grade': None, 'student': False, 'interest': '无'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f48'), 'name': '葫芦娃', 'age': 100, 'grade': None, 'student': False, 'interest': '喷火'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find().sort('age', 1)\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 查询并对age降序排序\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f48'), 'name': '葫芦娃', 'age': 100, 'grade': None, 'student': False, 'interest': '喷火'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f44'), 'name': '刘四娃', 'age': 16, 'grade': None, 'student': False, 'interest': '无'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f43'), 'name': '马三娃', 'age': 14, 'grade': '八年级', 'student': True, 'interest': '下棋'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f42'), 'name': '张二娃', 'age': 12, 'grade': '六年级', 'student': True, 'interest': '跳舞'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f41'), 'name': '李大娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '唱歌'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f47'), 'name': '赵气娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '乐高'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f46'), 'name': '高六娃', 'age': 8, 'grade': '一年级', 'student': True, 'interest': '学习'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find().sort('age', -1)\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 对字段去重\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "# 对字段去重\n",
    "handler.distinct('字段名')\n",
    "\n",
    "# 先筛选再去重\n",
    "handler.distinct('字段名', 查询条件)\n",
    "```\n",
    "\n",
    "### 注意\n",
    "\n",
    "如果字段的数量很大，那么轻易不要在Robo 3T里面直接执行，否则可能导致Robo 3T卡死。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 添加一些测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<pymongo.results.InsertManyResult at 0x10d3c0cf0>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = [\n",
    "    {'name': '阚九娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '魔术'},\n",
    "    {'name': '钱十娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '直播'},\n",
    "    {'name': '孙十一娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '打架'},\n",
    "    {'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'},\n",
    "    {'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'},\n",
    "]\n",
    "handler.insert_many(data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 对age进行去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 12, 14, 16, 18, 8, 100]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "handler.distinct('age')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 对age为18的数据中的grade字段去重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['高三', '四年级', '五年级']\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "rows = handler.distinct('grade', {'age': 18})\n",
    "\n",
    "print(rows)\n",
    "print(len(rows))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 逻辑查询\n",
    "\n",
    "### 基本符号\n",
    "\n",
    "| 符号 | 意义 |\n",
    "| --- | --- |\n",
    "| \\$gt | 大于 |\n",
    "| \\$gte | 大于等于 |\n",
    "| \\$lt | 小于 |\n",
    "| \\$lte | 小于等于 | \n",
    "| \\$ne | 不等于 |\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "collection.find({'字段名': {'基本符号': 边界值, '基本符号': 边界值}})\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 查询age 大于15的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f44'), 'name': '刘四娃', 'age': 16, 'grade': None, 'student': False, 'interest': '无'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f48'), 'name': '葫芦娃', 'age': 100, 'grade': None, 'student': False, 'interest': '喷火'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd659'), 'name': '阚九娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '魔术'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65a'), 'name': '钱十娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '直播'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65b'), 'name': '孙十一娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '打架'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65c'), 'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65d'), 'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'age': {'$gt': 15}})\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 查询age大于12小于等于18的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f43'), 'name': '马三娃', 'age': 14, 'grade': '八年级', 'student': True, 'interest': '下棋'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f44'), 'name': '刘四娃', 'age': 16, 'grade': None, 'student': False, 'interest': '无'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd659'), 'name': '阚九娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '魔术'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65a'), 'name': '钱十娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '直播'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65b'), 'name': '孙十一娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '打架'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65c'), 'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65d'), 'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'age': {'$gt': 12, '$lte': 18}})\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 查询age为18，年纪不为四年级的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65c'), 'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65d'), 'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'age': 18, 'grade': {'$ne': '四年级'}})\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 或查询\n",
    "\n",
    "### 基本语法\n",
    "\n",
    "```\n",
    "handler.find({'$or': [查询条件1, 查询条件2]})\n",
    "```\n",
    "\n",
    "查询条件1与查询条件2只要有一个满足即可"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 查询age为18或者grade为五年级的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f41'), 'name': '李大娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '唱歌'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f45'), 'name': '朱五娃', 'age': 18, 'grade': '高三', 'student': True, 'interest': '写字'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f47'), 'name': '赵气娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '乐高'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd659'), 'name': '阚九娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '魔术'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65a'), 'name': '钱十娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '直播'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65b'), 'name': '孙十一娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '打架'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65c'), 'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65d'), 'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'$or': [{'age': 18}, {'grade': '五年级'}]})\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 查询 age为100000或者grade为四年级或者grade为五年级的数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'_id': ObjectId('5b2f75d26b78a61364d09f41'), 'name': '李大娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '唱歌'}\n",
      "{'_id': ObjectId('5b2f75d26b78a61364d09f47'), 'name': '赵气娃', 'age': 10, 'grade': '五年级', 'student': True, 'interest': '乐高'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd659'), 'name': '阚九娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '魔术'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65a'), 'name': '钱十娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '直播'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65b'), 'name': '孙十一娃', 'age': 18, 'grade': '四年级', 'student': True, 'interest': '打架'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65c'), 'name': '李十二娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '模型'}\n",
      "{'_id': ObjectId('5c67a1d1a541dd46d3ebd65d'), 'name': '周十三娃', 'age': 18, 'grade': '五年级', 'student': True, 'interest': '手办'}\n"
     ]
    }
   ],
   "source": [
    "rows = handler.find({'$or': [{'age': 100000}, {'grade': '四年级'}, {'grade': '五年级'}]})\n",
    "for row in rows:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## 显式与查询\n",
    "\n",
    "### 隐式与查询\n",
    "\n",
    "```\n",
    "collection.find({'name': 'xxx', 'age': 12, 'grade': '五年级'})\n",
    "```\n",
    "\n",
    "### 显式与查询\n",
    "\n",
    "```\n",
    "collection.find({'$and': [{'name': 'xxx'}, {'age': 12}, {'grade': '五年级'}]})\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2019-02-16-09-59-28.png)\n",
    "![读者交流QQ群](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/2019-02-16-09-59-56.png)![微信公众号](https://kingname-1257411235.cos.ap-chengdu.myqcloud.com/wechatplatform.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
